package  
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author KhanhNHV
	 */
	public class SquareTextBlock extends MovieClip
	{
		public var timerInterval:Number;
		public var timer:Timer;
		//coordination of this blocks
		public var xCoord:Number = 0;
		public var yCoord:Number = 0;
		public var sizeSquare:Number = 50;
		public var index:Number = 0;
		public function SquareTextBlock(txt:String, xCoord:Number, yCoord:Number,specialCharIndex:Number) 
		{
			var i:Number;
			var sqrTxt :SquareText;
			for (i = 0; i < txt.length ; i++)
			{
				if (i == specialCharIndex)				
				sqrTxt = new SquareText(txt.charAt(i), sizeSquare, true)
				else
				sqrTxt = new SquareText(txt.charAt(i), sizeSquare, false)
				sqrTxt.x = 50 * i;
				this.addChild(sqrTxt);
			}
			//set timer
			timerInterval = 3000;
			timer = new Timer(timerInterval, 1);
			//set coordination
			this.xCoord = xCoord;
			this.yCoord = yCoord;
			this.x = this.xCoord * sizeSquare;
			this.y = this.yCoord * sizeSquare;
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, callChildWaitingText);
			this.addEventListener("OnGetFocused", callChildGetFocus);	
			this.addEventListener("OnCorrectAnswer", callChildRevealText);
			this.addEventListener("OnWrongAnswer", callChildDisableText);
			this.addEventListener("OnFocusTextCompleted", callParentDisplayTextQuestion);
			this.addEventListener("OnSpecialBlockFocused", dispatchParentSpecialBlockFocused);
		}
		
		private function  dispatchParentSpecialBlockFocused(evt:Event):void 
		{
			parent.dispatchEvent(new Event("SpecialBlockFocused"));
		}
		public function callChildGetFocus(evt:Event):void
		{
			for (var i = 0; i < this.numChildren; i++)
			{
				var child:SquareText = this.getChildAt(i) as SquareText;
				child.focusText(i);
				child.state = 1;
			}
			timer.start();
		
		}	
		
		public function callChildWaitingText(evt:TimerEvent)
		{
			for (var i = 0; i < this.numChildren; i++)
			{
				var child:SquareText = this.getChildAt(i) as SquareText;
				child.waitingText();
				child.state = 2;
			}
		
		}
		
		public function callChildRevealText(evt:Event)
		{
			for (var i = 0; i < this.numChildren; i++)
			{
				var child:SquareText = this.getChildAt(i) as SquareText;
				child.revealText(i);
				child.state = 3;
			}
		
		}
		
		public function callChildDisableText(evt:Event)
		{
			for (var i = 0; i < this.numChildren; i++)
			{
				var child:SquareText = this.getChildAt(i) as SquareText;
				child.disabledText(i);
				child.state = 4;
				
			}
		}
		
		public function callParentDisplayTextQuestion(evt:Event)
		{
			
			parent.dispatchEvent(new CustomEvent(CustomEvent.FOCUSTEXTCOMPLETED,true,true,this.index));
		}
		
		public function highlightSpecialSquare()
		{
			for (var i:Number = 0; i < this.numChildren; i++)
			{
				var sqrText : SquareText = this.getChildAt(i) as SquareText;
				if (sqrText.isSpecialBlock)
				{
					//highlight him
					sqrText.highlightSpecialBlock();
				}
			}
		}
		
	}

}